package Classic150;

import java.util.Arrays;

public class 合并区间 {
    /*
        sort方法排序规则的定义，数组的原地修改
     */
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
        int index = 0;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] <= intervals[index][1]) intervals[index][1] = Math.max(intervals[index][1], intervals[i][1]);
            else intervals[++index] = intervals[i];
        }
        return Arrays.copyOf(intervals, ++index);
    }

}
